<template>
  <div class="fy-login">
    <h1>后台管理系统</h1>
    <div class="fy-login-form">
      <el-form
        :model="formData"
        :rules="rules"
        ref="submitForm"
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input v-model="formData.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="formData.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            class="login-button"
            type="primary"
            @click.stop="handleSubmit('submitForm')"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
      <div class="login-p">
        <p class="text">温馨提示</p>
        <p class="text">未登录过的新用户，自动注册</p>
        <p class="text">注册过的用户可凭账号密码登录</p>
      </div>
    </div>
  </div>
</template>

<script>
import { mapActions, mapState } from "vuex";
export default {
  computed: {
    ...mapState(["adminInfo"]),
  },
  name: "Login",
  data: function () {
    return {
      formData: {
        username: "",
        password: "",
      },
      rules: {
        username: {
          required: true,
          message: "输入不能为空",
          trigger: "blur",
        },
        password: {
          required: true,
          message: "输入不能为空",
          trigger: "blur",
        },
      },
    };
  },
  methods: {
    ...mapActions(["getAdminData"]),
    handleSubmit(formName) {
      // ref submitForm
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios
            .post("/admin/login", {
              user_name: this.formData.username,
              password: this.formData.password,
            })
            .then((res) => {
              console.log(res);
              if (res.status === 1) {
                this.$message({
                  message: "登录成功",
                  type: "success",
                });
                // 登录成功进入 dashboard页面
                this.$router.push({ path: "dashboard" });
              } else {
                this.$message({
                  type: "error",
                  message: "登录错误！请检查网络或者账号密码！",
                });
              }
            });
        } else {
          this.$notify({
            title: "账号密码验证不通过！",
            message: "账号或者密码有误！",
            type: "warning",
            showClose: false,
          });
          return false;
        }
      });
    },
  },
  components: {},
  mounted() {
    // if (!this.adminInfo.id) {
    //   this.getAdminData();
    // }
  },
  watch: {
    adminInfo: function (newValue) {
      if (newValue.id) {
        this.$message({
          type: "success",
          message: "检测到您之前登录过，将自动登录",
        });
        this.$router.push("dashboard");
      }
    },
  },
};
</script>

<style lang="less">
@import url("../style/common.less");
.fy-login {
  .tlc();
  h1 {
    color: white;
    // color:white;
    text-align: center;
    font-weight: 200;
    padding: 20px;
  }

  .fy-login-form {
    border-radius: 10px;
    padding: 10px;
    background-color: white;
    .wh(300px,300px);
    .login-form {
      width: 90%;
      margin: 0 auto;
      .login-button {
        width: 100%;
      }
    }
  }
  .login-p {
    .text {
      text-align: center;
      color: red;
      font-size: 14px;
    }
  }
}
</style>
